package com.google.code.wicket.auth.component.strategy;

import org.apache.wicket.Component;
import org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener;

import com.google.code.wicket.auth.component.marker.Marker;

/**
 * An {@link IUnauthorizedComponentInstantiationListener} based an a
 * {@link Marker}.
 * 
 * @param <T>
 */
public class MarkerBasedUnauthorizedComponentInstatiationListener<T> implements
        IUnauthorizedComponentInstantiationListener {

    private IUnauthorizedComponentInstantiationListener wrappedStrategy;

    private final Marker<T> marker;

    /**
     * Constructor wrapping an existing strategy.
     * 
     * @param wrappedStrategy
     * @param marker
     */
    public MarkerBasedUnauthorizedComponentInstatiationListener(
            final IUnauthorizedComponentInstantiationListener wrappedStrategy,
            final Marker<T> marker) {
        super();
        this.wrappedStrategy = wrappedStrategy;
        this.marker = marker;
    }

    /**
     * Constructor with a marker.
     * 
     * @param marker
     */
    public MarkerBasedUnauthorizedComponentInstatiationListener(
            final Marker<T> marker) {
        this.marker = marker;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onUnauthorizedInstantiation(final Component component) {
        if (!this.marker.handleComponent(component)) {
            if (this.wrappedStrategy != null) {
                this.wrappedStrategy.onUnauthorizedInstantiation(component);
            }
        }
    }

}
